#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
#include "main.h"


#include "WorldStatusUpdate.pb.h"
#include "Commands.pb.h"
#include "ProtobufUtil.hh"

using namespace strategy_protocol;
using namespace protoutil;
using namespace std;

void TheStrategy::random_undiscovered(bool workers,string id)
{
	vector<Position> undiscovered;

	for(int i=0;i<M;i++)
		for(int j=0;j<N;j++)
			if(!discovered[i][j] && reachable_pos[i][j])
            {
				Position tmp;
				tmp.set_x(i+translation_x);
				tmp.set_y(j+translation_y);
                undiscovered.push_back(tmp);
			}

	if(undiscovered.size()==0)
		return;
	if(workers)
    {
        his_mine[id] = boss_found?boss_pos:base_pos;
        Position pos = workers_task[id].target_pos;
		if(discovered[pos.x()-translation_x][pos.y()-translation_y])
		{
			workers_task[id].task=MOVE;
			Position act_pos;
			for(int k=0;k<world.workers_size();k++)
                if(world.workers(k).common().id()==id)
                    act_pos = world.workers(k).common().position();
            int mini=0;
            for(unsigned int i=1;i<undiscovered.size();i++)
                if(distance(undiscovered[i],act_pos)<distance(undiscovered[mini],act_pos)) mini=i;
            workers_task[id].target_pos=undiscovered[mini];
            workers_task[id].approx=true;
        }
    }
	else
	{
		for(int k=0;k<world.soldiers_size();k++)
		{
			Position pos = soldiers_task[world.soldiers(k).common().id()].target_pos;
			if(discovered[pos.x()-translation_x][pos.y()-translation_y])
			{
			    Position act_pos=world.soldiers(k).common().position();
                int mini=0;
                for(unsigned int i=1;i<undiscovered.size();i++)
                    if(distance(undiscovered[i],act_pos)<distance(undiscovered[mini],act_pos)) mini=i;
				soldiers_task[world.soldiers(k).common().id()].task=MOVE;
				soldiers_task[world.soldiers(k).common().id()].target_pos=undiscovered[mini];
				soldiers_task[world.soldiers(k).common().id()].approx=true;
			}
		}
	}
}

//void TheStrategy::random_tour(WorldStatusUpdateMessage_Soldier myman)
//{
	// // cerr << "Expl pos " << explorer_tour.x()-translation_x << " " << explorer_tour.y()-translation_y << endl;
	// Position act_pos = myman.common().position();
	// Position new_pos = nextstep(act_pos,explorer_tour,true,false);
	// // cerr << "Act pos " << act_pos.x()-translation_x << " " <<  act_pos.y()-translation_y << endl;
	// // cerr << "New pos " << new_pos.x()-translation_x << " " << new_pos.y()-translation_y << endl;
	// if(new_pos.x()-translation_x==act_pos.x()-translation_x && new_pos.y()-translation_y==act_pos.y()-translation_y)
	// {
		// if(explorer_tour.x()-translation_x>3)
		// {
			// explorer_tour.set_x(explorer_tour.x()-translation_x-3);
			// explorer_tour.set_y(explorer_tour.y()-translation_y+3);
		// }
		// else
		// {
			// explorer_tour.set_x(explorer_tour.y()-translation_y+6);
			// explorer_tour.set_y(0);
		// }
		// // cerr << "New expl pos " << explorer_tour.x()-translation_x << " " << explorer_tour.y()-translation_y << endl;
	// }
	// else
	// {
		// movecmd(myman,new_pos);
	// }
	// // int delta_x, delta_y, i = -1;
	// // do
	// // {
		// // delta_x = rand() % 3 - 1;
		// // delta_y = rand() % 3 - 1;
		// // new_pos.set_x(act_pos.x()-translation_x+delta_x);
		// // new_pos.set_y(act_pos.y()-translation_y+delta_y);
		// // i++;
	// // }while(mymap[new_pos.x()-translation_x][new_pos.y()-translation_y]!=' ' && i<20);
	// // cerr<<"delta_x "<<delta_x<<endl;
	// // cerr<<"delta_y "<<delta_y<<endl;
	// // if(i<20)
		// // movecmd(id,new_pos);
//}

//void TheStrategy::random_tour_snail(WorldStatusUpdateMessage_Soldier myman)
//{
//	// cerr << "Expl pos " << explorer_tour.x()-translation_x << " " << explorer_tour.y()-translation_y << endl;
//	Position act_pos = myman.common().position();
//	Position new_pos = nextstep(act_pos,explorer_tour,true,false);
//	// cerr << "Act pos " << act_pos.x()-translation_x << " " <<  act_pos.y()-translation_y << endl;
//	// cerr << "New pos " << new_pos.x()-translation_x << " " << new_pos.y()-translation_y << endl;
//	if(new_pos.x()-translation_x==act_pos.x()-translation_x && new_pos.y()-translation_y==act_pos.y()-translation_y)
//	{
//		if(explorer_tour.x()-translation_x>3)
//		{
//			explorer_tour.set_x(explorer_tour.x()-translation_x-3);
//			explorer_tour.set_y(explorer_tour.y()-translation_y+3);
//		}
//		else
//		{
//			explorer_tour.set_x(explorer_tour.y()-translation_y+6);
//			explorer_tour.set_y(0);
//		}
//		// cerr << "New expl pos " << explorer_tour.x()-translation_x << " " << explorer_tour.y()-translation_y << endl;
//	}
//	else
//	{
//		movecmd(myman,new_pos);
//	}
//}

//void TheStrategy::defending_tour(WorldStatusUpdateMessage_Soldier myman,Position defending_pos)
//{
	// Position act_pos = myman.common().position();
	// int delta_x, delta_y;
	// delta_x = rand() % 5 - 2;
	// delta_y = rand() % 5 - 2;
	// defending_pos.set_x(defending_pos.x()-translation_x+delta_x);
	// defending_pos.set_y(defending_pos.y()-translation_y+delta_y);

	// Position new_pos = nextstep(act_pos,defending_pos,true,false);
	// movecmd(myman,new_pos);
//}

//void TheStrategy::everybody_random_tour()
//{
//	for(int j = 0; j < min(2,world.soldiers_size());++j)
//		random_tour(world.soldiers(j));
//	for(int j = 2; j < world.soldiers_size();++j)
//		defending_tour(world.soldiers(j),base_pos);
//}
